<?php
namespace App\Api\Controllers\Lianhou;

use App\Api\Controllers\lianhou\BaseController as base;
use Illuminate\Support\Facades\Log;
use function EasyWeChat\Kernel\Support\get_client_ip;
class PayController extends BaseController
{
    public function __construct()
    {

    }


    public function scan_pay($data, $config)
    {
        try {
            $params = [
                'mercCode' => $data['mch_id'],
                'termNo' => $data['term_no'],
                'outTradeNo' => $data['out_trade_no'],
                'orderAmount' => $data['total_amount'],
                'payCode' => $data['code'],
                'termIp' => get_client_ip(),
                'notifyUrl' => url('/api/lianhou/pay_notify_url'),
            ];
            $base = base::getInstance();

            $ret  = $base->params($params, $config)->build($base::SCAN_PAY, $config, 'scan_pay');

            if (isset($ret['payStatus']) && !empty($ret['payStatus'])) {
                //PayResult 0成功，非0失败
                if ($ret['payStatus'] == "SUCCESS") { //支付成功
                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret
                    ];
                } else if ($ret['payStatus'] == 'PROCESSING') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['failMessage'])) {
                        $message = $ret['failMessage'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }


    //查询订单 0-系统错误 1-成功 2-正在支付 3-失败 4.已经退款
    public function order_query($data, $config)
    {
        try {
            $params = [
                'outTradeNo' => $data['out_trade_no']
            ];
            $base = base::getInstance();

            $ret  = $base->params($params, $config)->build($base::QUERY_ORDER, $config, 'order_query');

            if (isset($ret['payStatus']) && !empty($ret['payStatus'])) {
                //PayResult 0成功，非0失败
                if ($ret['payStatus'] == "SUCCESS") { //支付成功
                    return [
                        'status' => 1,
                        'message' => '支付成功',
                        'data' => $ret
                    ];
                } else if ($ret['payStatus'] == 'PROCESSING') {
                    return [
                        'status' => 2,
                        'message' => '支付中',
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['failMessage'])) {
                        $message = $ret['failMessage'];
                    }
                    return [
                        'status' => 3,
                        'message' => $message,
                    ];
                }
            } else {
                return [
                    'status' => 3,
                    'message' => '支付异常',
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }


    //退款 0-系统错误 1-成功
    public function refund($data, $config)
    {
        try {
            $params = [
                'payOutTradeNo' => $data['out_trade_no'],
                'refundAmount' => $data['refund_amount'],
                'outTradeNo' => $data['refund_no']
            ];
            $base = base::getInstance();

            $ret  = $base->params($params, $config)->build($base::REFUND, $config, 'refund');

            if (isset($ret['refundStatus']) && !empty($ret['refundStatus'])) {
                if ($ret['refundStatus'] == 'REQUEST_SUCCESS') {
                    return [
                        'status' => 1,
                        'message' => '退款成功',
                        'data' => [
                            'refund_count' => 1,
                            'out_trade_no'  => $data['out_trade_no'],
                        ]
                    ];
                } else {
                    $message = '失败了';
                    if (isset($ret['respMsg'])) {
                        $message = $ret['respMsg'];
                    }
                    return [
                        'status' => 2,
                        'message' => $message
                    ];
                }
            } else {
                $message = '失败了';
                if (isset($ret['respMsg'])) {
                    $message = $ret['respMsg'];
                }
                return [
                    'status' => 2,
                    'message' => $message
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 0,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }


    //退款查询 0-系统错误 1-成功 2-正在退款 3-失败
    public function refund_query($data)
    {
        try {


        } catch (\Exception $ex) {
            return [
                'status' => '0',
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine()
            ];
        }
    }

    public function qr_submit($data, $config)
    {
        try {
            $params = [
                'mercCode' => $data['mch_id'],
                'termNo' => $data['term_no'],
                'outTradeNo' => $data['out_trade_no'],
                'orderAmount' => $data['total_amount'],
                'clientType' => 'PUBLIC',
                'termIp' => get_client_ip(),
                'notifyUrl' => url('/api/lianhou/pay_notify_url'),
            ];

            $base = base::getInstance();

            $ret  = $base->params($params, $config)->build($base::QR_SUBMIT, $config, 'qr_submit');

            if (isset($ret['redirectUrl']) && !empty($ret['redirectUrl'])) {
                return [
                    'status' => 1,
                    'message' => '支付成功',
                    'data' => [
                        'codeUrl' => $ret['redirectUrl']
                    ]
                ];
            } else {
                $message = '失败了';
                if (isset($ret['respMsg'])) {
                    $message = $ret['respMsg'];
                }
                return [
                    'status' => 3,
                    'message' => $message,
                ];
            }
        } catch (\Exception $ex) {
            return [
                'status' => 3,
                'message' => $ex->getMessage().' | '.$ex->getFile().' | '.$ex->getLine(),
            ];
        }
    }





}
